// Functions associated with the Window class
//-----------------

//-----------------
// Includes libraries and other files
#include "Window.h"

//-----------------

//-----------------
//// CONSTRUCTOR - Window
Window::Window() {	
	// forward referencing for function
	LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

	// register the window class
	WNDCLASSEX wc_ = {sizeof( WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle( NULL), NULL, NULL, NULL, NULL, "Initial-Strike", NULL};
	RegisterClassEx( &wc_);

	// create the application's window
	hWnd_ = CreateWindow( "Initial-Strike", "Initial-Strike", WS_EX_TOPMOST, 0, 0, 0, 0, NULL, NULL, wc_.hInstance, NULL);

	// setup initial screen values
	screen_.SCREEN_WIDTH = GetSystemMetrics( SM_CXFULLSCREEN);
	// should be CYFULLSCREEN but value returned is wrong; for some reason this works.
	// add 20 to correct position of the H.U.D.
	screen_.SCREEN_HEIGHT = GetSystemMetrics( SM_CYSCREEN);// + 20;
	screen_.pos_x = 0;
	screen_.pos_y = 0;

	DEVMODE devm;
	devm.dmSize = sizeof( DEVMODE);
	devm.dmPelsWidth = screen_.SCREEN_WIDTH;
	devm.dmFields |= DM_PELSWIDTH;
	devm.dmPelsHeight = screen_.SCREEN_HEIGHT;// - 20;  // deduct H.U.D. correction to use correct values
	devm.dmFields |= DM_PELSHEIGHT;

	// change resolution
	ChangeDisplaySettings( &devm, 0);

	// change to fullscreen style
	SetWindowLongPtr( hWnd_, GWL_EXSTYLE, WS_EX_APPWINDOW);
	SetWindowLongPtr( hWnd_, GWL_STYLE, WS_POPUP);

	// move and resize the screen
	SetWindowPos( hWnd_, HWND_TOP, screen_.pos_x, screen_.pos_y, screen_.SCREEN_WIDTH, screen_.SCREEN_HEIGHT, SWP_FRAMECHANGED);

	// maximize the window
	ShowWindow( hWnd_, SW_MAXIMIZE);
	SetFocus( hWnd_);

	SetCapture( hWnd_);
}

//// DESTRUCTOR - Window
Window::~Window() {
	ReleaseCapture();
}
//-----------------

//-----------------
//// FUNCTION - get_hWnd
// retrieve the window
HWND Window::get_hWnd() const {
	// Pre-conditions :- none
	assert( true);

	return hWnd_;
}

//// FUNCTION - get_screen_width
// retrieve the width of the window
int Window::get_screen_width() const {
	// Pre-conditions :- none
	assert( true);

	return screen_.SCREEN_WIDTH;
}

//// FUNCTION - get_screen_height
// retrieve the height of the window
int Window::get_screen_height() const {
	// Pre-conditions :- none
	assert( true);

	return screen_.SCREEN_HEIGHT;
}

//// FUNCTION - invert_y
// invert the y position using the window height
int Window::invert_y( float y) const {
	// Pre-conditions :- none
	assert( true);

	return( screen_.SCREEN_HEIGHT - y);
}

//// FUNCTION - unregister
// unregister the window in preparation for it to be deleted
void Window::unregister() const {
	// Pre-conditions :- none
	assert( true);

	UnregisterClass( "Initial-Strike", wc_.hInstance);
}
//-----------------

//-----------------
//// FUNCTION - WINAPI MsgProc
// message handling function for the window
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	// Pre-conditions :- none
	assert( true);

	switch( msg) {
		case WM_DESTROY: {
			// respond to a Windows destroy event
			// usually generated by clicking on the close box
			PostQuitMessage( 0);
			return 0;
		}
	}

	return DefWindowProc( hWnd, msg, wParam, lParam);
}